package com.ease.platform.cluster.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;

import com.ease.common.util.Detect;
import com.ease.platform.authorization.service.impl.AbstractAuthorizationServiceImpl;
import com.ease.platform.cluster.support.ClusterBoardcastHttpServiceCallback;
import com.ease.platform.cluster.support.ClusterBoardcastRmiServiceCallback;

public abstract class AbstractClusterBoardcastServiceImpl extends AbstractAuthorizationServiceImpl implements InitializingBean {

	private static final transient Log log = LogFactory.getLog(AbstractClusterBoardcastServiceImpl.class);

	private String[] endpointList;

	protected String endpoints;

	// <bean id="httpInvokerProxyFactoryBean" class=
	// "org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
	// <!-- property name="serviceUrl"
	// value="http://localhost:8080/spring/service"
	// /> <property name="serviceInterface"
	// value="org.zlex.spring.httpinvoke.service.UserService"
	// / -->
	// <property name="httpInvokerRequestExecutor"
	// ref="httpInvokerRequestExecutor" />
	// </bean>
	@SuppressWarnings("unchecked")
	protected <T> List<Object> invoke(ClusterBoardcastHttpServiceCallback<T> clusterBoardcastHttpServiceCallback, String serviceName) {
		List<Object> objects = new ArrayList<Object>();
		if (Detect.notEmpty(endpointList)) {
			for (String endpoint : endpointList) {
				try {
					HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
					httpInvokerProxyFactoryBean.setServiceInterface(clusterBoardcastHttpServiceCallback.getServiceInterface());
					// rmi://127.0.0.1:1099/userSessionManagementRmiService
					httpInvokerProxyFactoryBean.setServiceUrl("http://" + endpoint + "/remoting/" + serviceName);
					httpInvokerProxyFactoryBean.afterPropertiesSet();
					Object rmiService = httpInvokerProxyFactoryBean.getObject();
					objects.add(clusterBoardcastHttpServiceCallback.execute((T) rmiService));
				} catch (Exception e) {
					log.info(e.getMessage());
				}
			}
		}

		return objects;
	}

	@SuppressWarnings("unchecked")
	protected <T> List<Object> invoke(ClusterBoardcastRmiServiceCallback<T> clusterBoardcastRmiServiceCallback, String serviceName) {
		List<Object> objects = new ArrayList<Object>();
		if (!Detect.notEmpty(endpointList)) {
			return objects;
		}
		for (String endpoint : endpointList) {
			RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
			// rmiProxyFactoryBean.setServiceInterface(ReflectionUtil.
			// getGenericType(clusterBoardcastRmiServiceCallback.getClass()));
			rmiProxyFactoryBean.setServiceInterface(clusterBoardcastRmiServiceCallback.getServiceInterface());
			// rmi://127.0.0.1:1099/userSessionManagementRmiService
			rmiProxyFactoryBean.setServiceUrl("rmi://" + endpoint + "/" + serviceName);
			Object rmiService = rmiProxyFactoryBean.getObject();
			objects.add(clusterBoardcastRmiServiceCallback.execute((T) rmiService));
		}
		return objects;
	}

	//
	// <bean id="helloBuddyService"
	// class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
	// <property name="serviceUrl"
	// value="rmi://127.0.0.1:1099/userSessionManagementRmiService"/>
	// <property name="serviceInterface"
	// value="com.irm.system.usersession.service.UserSessionManagementService"/>
	// </bean>

	public void afterPropertiesSet() throws Exception {
		if (Detect.notEmpty(endpoints)) {
			endpointList = endpoints.split(";");
		}
	}

	public String[] getEndpointList() {
		return endpointList;
	}

	public void setEndpointList(String[] endpointList) {
		this.endpointList = endpointList;
	}

	public String getEndpoints() {
		return endpoints;
	}

	public void setEndpoints(String endpoints) {
		this.endpoints = endpoints;
	}

}
